




import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class Student extends JFrame implements ActionListener{
	

	JPanel jp_1, jp_2, jp_3, jp_4, jp_5, jp_6 = null;
	JLabel jlb_1, jlb_2, jlb_3, jlb_4 = null;
	JButton jb_1, jb_2, jb_3, jb_4 = null;
	JTable jtable_1, jtable_2 = null;
	DefaultTableModel model_1, model_2 = null;
	JScrollPane jsp_1, jsp_2 = null;



	String Sno;
	String math;
	String c;
	String java;
	String english;

	String math_room;
	String c_room;
	String java_room;
	String english_room;



	static String dbURL = "jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false";


	static String userName="root";
	static String userPwd="0118";


	static Connection ct = null;
	PreparedStatement ps = null;
	ResultSet rs = null;



	Student(String name,String sno){
		Sno = sno;


		try {
			DataSource dataSource = new MysqlDataSource();
			((MysqlDataSource) dataSource).setURL(dbURL);
			((MysqlDataSource) dataSource).setUser(userName);
			((MysqlDataSource) dataSource).setPassword(userPwd);
			ct = dataSource.getConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}



		jp_1 = new JPanel();
		jp_2 = new JPanel();
		jp_3 = new JPanel();
		jp_4 = new JPanel();
		jp_5 = new JPanel();
		jp_6 = new JPanel();


		jlb_1 = new JLabel("姓名：");
		jlb_2 = new JLabel(name);
		jlb_3 = new JLabel("学号：");
		jlb_4 = new JLabel(sno);

		jlb_1.setFont(new java.awt.Font("Dialog", 1, 30));
		jlb_2.setFont(new java.awt.Font("Dialog", 1, 25));
		jlb_3.setFont(new java.awt.Font("Dialog", 1, 30));
		jlb_4.setFont(new java.awt.Font("Dialog", 1, 25));


		jb_1 = new JButton("考场查询");
		jb_2 = new JButton("成绩查询");
		jb_3 = new JButton("退出系统");
		jb_4 = new JButton("修改密码");
		jb_1.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_2.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_3.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_4.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_1.setBackground(Color.GRAY);
		jb_2.setBackground(Color.GRAY);
		jb_3.setBackground(Color.GRAY);
		jb_4.setBackground(Color.GRAY);


		jb_1.addActionListener(this);
		jb_2.addActionListener(this);
		jb_3.addActionListener(this);
		jb_4.addActionListener(this);


		String[] colnames = {"高数成绩", "C语言成绩", "Java成绩", "英语成绩" };
		model_2 = new DefaultTableModel(colnames,1);
		jtable_2 = new JTable(model_2);
		jsp_2 = new JScrollPane(jtable_2);

		String[] colnames_1 = {"高数考场", "C语言考场", "Java考场", "英语考场"};
		model_1 = new DefaultTableModel(colnames_1, 1);
		jtable_1 = new JTable(model_1);
		jsp_1 = new JScrollPane(jtable_1);


		jp_1.add(jlb_1);
		jp_1.add(jlb_2);

		jp_2.add(jlb_3);
		jp_2.add(jlb_4);

		jp_3.add(jb_1);
		jp_3.add(jsp_1);

		jp_4.add(jb_2);
		jp_4.add(jsp_2);

		jp_5.add(jb_4);
		jp_5.add(jb_3);


		this.add(jp_1);
		this.add(jp_2);
		this.add(jp_3);
		this.add(jp_4);
		this.add(jp_5);


		this.setLayout(new GridLayout(5, 1));
		jp_1.setLayout(new FlowLayout(FlowLayout.LEFT));
		jp_2.setLayout(new FlowLayout(FlowLayout.LEFT));


		this.setTitle("学生系统");

		URL resource = Login.class.getClassLoader().getResource("河池学院.jfif");
		Image image = new ImageIcon(resource).getImage();
		this.setIconImage(image);


		this.setSize(500, 500);
		this.setBackground(Color.yellow);

		this.setLocationRelativeTo(null);


		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		this.setVisible(true);
		this.setResizable(true);
	}



	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == "考场查询"){
			try{
				ps = ct.prepareStatement("select * from Student where 学号 =? ");
				ps.setString(1, Sno);
				rs = ps.executeQuery();


				while(rs.next()){
					math_room = rs.getString(10);
					c_room = rs.getString(11);
					java_room = rs.getString(12);
					english_room = rs.getString(13);
				}
			}catch (Exception e1) {
			e1.printStackTrace();
			}

			jtable_1.setValueAt(math_room, 0, 0);
			jtable_1.setValueAt(c_room, 0, 1);
			jtable_1.setValueAt(java_room, 0, 2);
			jtable_1.setValueAt(english_room, 0, 3);
		}else if(e.getActionCommand() == "成绩查询"){
			try{
				ps = ct.prepareStatement("select * from Student where 学号 =? ");
				ps.setString(1, Sno);
				rs = ps.executeQuery();

				while(rs.next()){
					math = rs.getString(6);
					c = rs.getString(7);
					java = rs.getString(8);
					english = rs.getString(9);
				}
			}catch (Exception e1) {
			e1.printStackTrace();
			}

			jtable_2.setValueAt(math, 0, 0);
			jtable_2.setValueAt(c, 0, 1);
			jtable_2.setValueAt(java, 0, 2);
			jtable_2.setValueAt(english, 0, 3);
		}else if(e.getActionCommand() == "修改密码"){
			ChangePassword cp = new ChangePassword(Sno);
		}else if(e.getActionCommand() == "退出系统"){
			System.exit(0);
		}	
	}
}

